//
//  MOWaitDoDetail_MainViewController.m
//  MobileOffice
//
//  Created by 冰冰 on 2019/6/7.
//  Copyright © 2019 com.idolmedia. All rights reserved.
//。正文附件

#import "MOWaitDoDetail_MainViewController.h"
#import "MOWaitDoDetail_MainViewCell.h"
#import "MOHTMLLoadViewController.h"

@interface MOWaitDoDetail_MainViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *tableView;
//数据数组
@property(nonatomic,strong)NSMutableArray *dataArray;


@end

@implementation MOWaitDoDetail_MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.tableView];
    
    [self initData];
}

-(void)initData
{
//    self.dataArray = [NSMutableArray array];
//    NSArray *titleArr = @[@"附件1",@"附件2"];
//    NSMutableArray *muArr = [NSMutableArray array];
//    for (int i = 0; i<titleArr.count; i++) {
//        
//        MOWaitDoDetail_MainModel *model = [[MOWaitDoDetail_MainModel alloc]init];
//        model.name = titleArr[i];
//        
//        [muArr addObject:model];
//    
//    }
//    [self.dataArray addObject:@{@"arr":muArr,@"isOpen":@"1"}];
//    [self.tableView reloadData];
}

-(void)setEaitDoDetail_Main_DataArray:(NSMutableArray *)eaitDoDetail_Main_DataArray
{
    _eaitDoDetail_Main_DataArray = eaitDoDetail_Main_DataArray;
    
    self.dataArray = [NSMutableArray array];
    NSMutableArray *muArr = [NSMutableArray array];
    for (int i = 0; i<eaitDoDetail_Main_DataArray.count; i++) {
        NSDictionary *dic = eaitDoDetail_Main_DataArray[i];
        MOWaitDoDetail_MainModel *model = [[MOWaitDoDetail_MainModel alloc]init];
        model.alias = dic[@"alias"];
        model.docid = dic[@"docid"];
        model.domain = dic[@"domain"];
        model.mc = dic[@"mc"];
        model.name = dic[@"name"];
        model.parentid = dic[@"parentid"];
        model.path = dic[@"path"];
        model.xh = dic[@"xh"];
        [muArr addObject:model];
    }
    [self.dataArray addObject:@{@"arr":muArr,@"isOpen":@"0"}];
    
    [self.tableView reloadData];
}

#pragma mark -- 正文
-(void)clickRightButton:(UIButton *)sender
{
    if (sender.tag==1) {
        
        sender.selected = !sender.selected;
        if (sender.selected==YES) {
            NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:self.dataArray[0]];
            [dic setObject:@"1" forKey:@"isOpen"];
            [self.dataArray replaceObjectAtIndex:0 withObject:dic];
        }else{
            NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:self.dataArray[0]];
            [dic setObject:@"0" forKey:@"isOpen"];
            [self.dataArray replaceObjectAtIndex:0 withObject:dic];
        }
        [self.tableView reloadData];
        
    }else{
        
        NSString *url = [NSString stringWithFormat:@"%@/%@?authorization=%@",ODPSDB_ODPSQUERYWJCONTENT_URL,self.waitModel.key,[NSString URLEncodedString:[MOUser sharedMOUser].token]];
        if ([self.waitModel.comeStr isEqualToString:@"部门传阅"]) {
            url = [NSString stringWithFormat:@"%@/%@?authorization=%@",ODPSDB_ODPSGETBMCYZW_URL,self.waitModel.key,[NSString URLEncodedString:[MOUser sharedMOUser].token]];
        }
        
        MOHTMLLoadViewController *vc = [[MOHTMLLoadViewController alloc]init];
        vc.urlStr = url;
        /*
         从哪个页面来
         1.待办正文
         2.待办附件
         3.已办正文
         4.已办附件
         5.查询正文
         6.查询附件
         7.局内
         */
        vc.comeType = @"1";
        [self.navigationController pushViewController:vc animated:YES];
        
    }
    
}

#pragma mark -- UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSArray *arr = self.dataArray[0][@"arr"];
    if (arr.count>0) {
        return 2;
    }else{
        return 1;
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section==0) {
        return 0;
    }else{
        NSArray *arr = self.dataArray[0][@"arr"];
        if ([self.dataArray[0][@"isOpen"] integerValue]==1) {
            return arr.count;
        }else{
            return 0;
        }
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MOWaitDoDetail_MainViewCell *cell = [MOWaitDoDetail_MainViewCell cellWithTableView:tableView andIndexPath:indexPath];
    NSArray *arr = self.dataArray[0][@"arr"];
    MOWaitDoDetail_MainModel *model = arr[indexPath.row];
    cell.model = model;
    return cell;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *arr = self.dataArray[0][@"arr"];
    MOWaitDoDetail_MainModel *model = arr[indexPath.row];
    return [MOWaitDoDetail_MainViewCell heightForMOWaitDoDetail_MainViewCellAndModel:model];
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.00001;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return kHConversions(54)+10;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, kHConversions(54)+10)];
    view.backgroundColor = KGrayColor_ededed;
    
    UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 10, SCREEN_WIDTH, kHConversions(54))];
    backView.backgroundColor = [UIColor whiteColor];
    [view addSubview:backView];
    
    UIButton *leftButton = [[UIButton alloc]initWithFrame:CGRectMake(17, 0, 80, kHConversions(54))];
    if (section==0) {
        [leftButton setTitle:@" 正文" forState:UIControlStateNormal];
        [leftButton setImage:IMAGE(@"zhenwen") forState:UIControlStateNormal];
    }else{
        [leftButton setTitle:@" 附件" forState:UIControlStateNormal];
        [leftButton setImage:IMAGE(@"fujian") forState:UIControlStateNormal];
    }
//    [leftButton setBackgroundColor:[UIColor redColor]];
    [leftButton setContentHorizontalAlignment:(UIControlContentHorizontalAlignmentLeft)];
    [leftButton setTitleColor:KBlackColor_2A2A2A forState:UIControlStateNormal];
    leftButton.titleLabel.font = FONT_SIZE(15);
    [backView addSubview:leftButton];
    
    CGFloat rightW = SCREEN_WIDTH - 34 - leftButton.width;
    UIButton *rightButton = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-17-rightW, 0, rightW, kHConversions(54))];
    if (section==1) {
        if ([self.dataArray[0][@"isOpen"] integerValue]==1) {
            rightButton.selected = YES;
        }else{
            rightButton.selected = NO;
        }
        [rightButton setImage:IMAGE(@"top_notice") forState:UIControlStateSelected];
    }
//    [rightButton setBackgroundColor:[UIColor greenColor]];
    [rightButton setImage:IMAGE(@"right_notice") forState:UIControlStateNormal];
    [rightButton setContentHorizontalAlignment:(UIControlContentHorizontalAlignmentRight)];
    
    rightButton.tag = section;
    [rightButton addTarget:self action:@selector(clickRightButton:) forControlEvents:UIControlEventTouchUpInside];
    
    [backView addSubview:rightButton];
    
    return view;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MOWaitDoDetail_MainModel *model = self.dataArray[0][@"arr"][indexPath.row];
    NSString *url = [NSString stringWithFormat:@"%@/%@?filepath=%@&filename=%@&authorization=%@",ODPSDB_ODPSWATCHDOCUMENTSU_URL,model.domain,model.path,model.name,[NSString URLEncodedString:[MOUser sharedMOUser].token]];
    
    MOHTMLLoadViewController *vc = [[MOHTMLLoadViewController alloc]init];
    vc.urlStr = url;
    /*
     从哪个页面来
     1.待办正文
     2.待办附件
     3.已办正文
     4.已办附件
     5.查询正文
     6.查询附件
     7.局内
     */
    vc.comeType = @"2";
    vc.domain = model.domain;
    vc.filePath = model.path;
    vc.fileName = [NSString passNullForString:model.name];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - 设置顶头分割线
-(void)viewDidLayoutSubviews {
    
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)])  {
        if (@available(iOS 8.0, *)) {
            [self.tableView setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];
        } else {
            // Fallback on earlier versions
        }
    }
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPat{
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        if (@available(iOS 8.0, *)) {
            [cell setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];
        } else {
            // Fallback on earlier versions
        }
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
        [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
}


#pragma mark -- 懒加载
-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - StatusBarAndNavigationBarHeight - 50 - 41 - TabbarSafeBottomMargin) style:(UITableViewStyleGrouped)];
        _tableView.backgroundColor = KGrayColor_ededed;
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}


@end
